﻿using System;
using System.Linq;
using BLL;
using DAL_NhaSach;

namespace BLL_NhaSach
{
	public class ChucVuBLL : IProcessData
	{
		QLNhaSachEntities db;
		public ChucVuBLL(string cnStr)
		{
			db = new QLNhaSachEntities(cnStr);
		}

		public object getAllData()
		{
			return (from cv in db.ChucVus select new { cv.MaChucVu, cv.TenChucVu, cv.QLHeThong, cv.QLKhoSach, cv.QLNhanVien, cv.QLNhapXuat, cv.QLThuChi, cv.BanSach });
		}

		public object findObjects(string find_content)
		{
			throw new NotImplementedException();
		}

		public object findObjects(int find_content)
		{
			return (db.ChucVus.SingleOrDefault(p => p.MaChucVu == find_content));
		}

		public object sort(string Col_Name, bool state)
		{
			throw new NotImplementedException();
		}

		public bool addObject(object addObj)
		{
			ChucVu cv = (ChucVu)addObj;
			if (cv != null)
			{
				try
				{
					db.ChucVus.AddObject(cv);
					db.SaveChanges();
					return true;
				}
				catch (Exception)
				{
					return false;
				}
			}
			else return false;
		}

		public bool deleteObject(int id)
		{
			ChucVu cv = db.ChucVus.SingleOrDefault(p => p.MaChucVu == id);
			if (cv != null)
			{
				try
				{
					db.ChucVus.DeleteObject(cv);
					db.SaveChanges();
					return true;
				}
				catch (Exception)
				{
					return false;
				}
			}
			else return false;
		}

		public bool updateObject(object updateObj)
		{
			ChucVu cv_new = (ChucVu)updateObj;
			if (cv_new != null)
			{
				try
				{
					ChucVu cv_old = db.ChucVus.SingleOrDefault(p => p.MaChucVu == cv_new.MaChucVu);
					cv_old.TenChucVu = cv_new.TenChucVu;
					cv_old.QLHeThong = cv_new.QLHeThong;
					cv_old.QLKhoSach = cv_new.QLKhoSach;
					cv_old.QLNhanVien = cv_new.QLNhanVien;
					cv_old.QLNhapXuat = cv_new.QLNhapXuat;
					cv_old.QLThuChi = cv_new.QLThuChi;
					cv_old.BanSach = cv_new.BanSach;
					db.SaveChanges();
					return true;
				}
				catch (Exception)
				{
					return false;
				}
			}
			else return false;
		}
        public bool check_QLHT(int maCV)
        {
            ChucVu cv= db.ChucVus.Where(c=>c.MaChucVu==maCV).FirstOrDefault();
            if (cv.QLHeThong == true)
                return true;
            return false;
        }
        public bool check_QLNV(int maCV)
        {
            ChucVu cv = db.ChucVus.Where(c => c.MaChucVu == maCV).FirstOrDefault();
            if (cv.QLNhanVien == true)
                return true;
            return false;
        }
        public bool check_QLTC(int maCV)
        {
            ChucVu cv = db.ChucVus.Where(c => c.MaChucVu == maCV).FirstOrDefault();
            if (cv.QLThuChi == true)
                return true;
            return false;
        }
        public bool check_QLKS(int maCV)
        {
            ChucVu cv = db.ChucVus.Where(c => c.MaChucVu == maCV).FirstOrDefault();
            if (cv.QLKhoSach == true)
                return true;
            return false;
        }
        public bool check_QLNX(int maCV)
        {
            ChucVu cv = db.ChucVus.Where(c => c.MaChucVu == maCV).FirstOrDefault();
            if (cv.QLNhapXuat == true)
                return true;
            return false;
        }
        public bool check_BanSach(int maCV)
        {
            ChucVu cv = db.ChucVus.Where(c => c.MaChucVu == maCV).FirstOrDefault();
            if (cv.BanSach == true)
                return true;
            return false;
        }
	}
}
